package com.kordar.admin.modular.sys.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.kordar.admin.enums.EnumUtils;
import com.kordar.admin.enums.ManagerStatus;
import com.kordar.admin.enums.YorN;
import com.kordar.admin.kernel.wrapper.BatchDeleteWrapperEntity;
import com.kordar.admin.kernel.wrapper.UpdateWrapperEntity;
import com.kordar.admin.modular.sys.entity.DictTypeEntity;
import com.kordar.admin.modular.sys.service.DictTypeService;
import com.kordar.datalist.DatalistService;
import com.kordar.datalist.ResultEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Set;

@Controller
@RequestMapping("sys")
public class DictTypeController {

    final
    DictTypeService dictTypeService;

    final
    DatalistService datalistService;

    public DictTypeController(DatalistService datalistService, DictTypeService dictTypeService) {
        this.datalistService = datalistService;
        this.dictTypeService = dictTypeService;
    }

    @ModelAttribute("ynLabels")
    public Map<String, String> yn() {
        return EnumUtils.EnumToMapString(YorN.class);
    }
    
    @ModelAttribute("statusLabels")
    public Map<String, String> status() {
        return EnumUtils.EnumToMapString(ManagerStatus.class);
    }

    @GetMapping("dicttype/index")
    public String index(Model model) {
        model.addAttribute("data",  datalistService.render(dictTypeService.list(), DictTypeEntity.class));
        return "pages/modular/system/dicttype/index";
    }

    @GetMapping("dicttype/form")
    public String form(Model model, @RequestParam(required = false, defaultValue = "0") Long id) {
        if (id == 0) {
            model.addAttribute("header", "新建项目");
            model.addAttribute("dicttype", new DictTypeEntity());
        } else {
            model.addAttribute("header", "修改项目");
            model.addAttribute("dicttype", dictTypeService.getById(id));
        }

        return "pages/modular/system/dicttype/form";
    }

    @PostMapping("dicttype/save")
    @ResponseBody
    public ResultEntity<Object> save(@Validated DictTypeEntity dictTypeEntity) {
        if (dictTypeEntity.getDictTypeId() == 0) {
            dictTypeEntity.setDictTypeId(null);
            dictTypeService.save(dictTypeEntity);
        } else {
            dictTypeService.updateById(dictTypeEntity);
        }
        return new ResultEntity<>();
    }

    @RequestMapping("dicttype/edit")
    @ResponseBody
    public ResultEntity<Object> edit(@RequestBody UpdateWrapperEntity<DictTypeEntity> entity) {
        UpdateWrapper<DictTypeEntity> query = entity.where(DictTypeEntity.class).data().getQuery();
        dictTypeService.update(query);
        return new ResultEntity<>();
    }

    @PostMapping("dicttype/delete")
    @ResponseBody
    public ResultEntity<Object> del(long id) {
        dictTypeService.removeById(id);
        return new ResultEntity<>();
    }

    @RequestMapping("dicttype/batch-delete")
    @ResponseBody
    public ResultEntity<Object> del(@RequestBody BatchDeleteWrapperEntity<Long> entity) {
        Set<Long> ids = entity.getIds();
        dictTypeService.removeByIds(ids);
        return new ResultEntity<>();
    }

}
